<?php
/**
 * Created by PhpStorm.
 * User: xmy2
 * Date: 18-8-11
 * Time: 上午10:45
 */

namespace vola;

class Controller
{
    /**
     * @var Request
     */
    public $req, $request;
    /**
     * @var Env
     */
    public $env;
    /**
     * @var Config
     */
    public $config;

    public function __construct()
    {
        $this->request = $this->req = Container::get('request');
        $this->env = Container::get('env');
        $this->config = Container::get('config');
        $this->runner();
    }

    protected function runner() {

    }

    public function controller($runner) {
        return App::runStatic($runner);
    }

    public function assign($name, $value = '') {
        Container::get('view')->assign($name, $value);
    }

    public function display($name = '', $assign = []) {
        if (empty($name)) {
            $name = debug_backtrace();
            $name = $name[1]['function'];
        }
        return Container::get('view')->display($name, $assign);
    }

    public function hasTpl($name) {
        return Container::get('view')->has_tpl($name);
    }

    public function retype($data, $type = 'text/html', $option = []) {
        return Container::get('response')->head($data, 200, $option)->content_type($type);
    }

    final public function success($msg = 'success', $url = '', $sec = 5)
    {
        $type = '(^_^)';
        $color = '43AEFA';
        $go = "立即跳转";
        include $this->env->get('root_path') . "tpl/redirect.php";
    }

    final public function error($msg = 'error', $url = '', $sec = 5)
    {
        $type = '(T_T)';
        $color = 'fa2f22';
        $go = "立即跳转";
        include $this->env->get('root_path') . "tpl/redirect.php";
    }

    final public function unknow($msg = 'error', $url = '', $sec = 5)
    {
        $type = '(?_?)';
        $color = 'fafa25';
        $go = "立即跳转";
        include $this->env->get('root_path') . "tpl/redirect.php";
    }

    final public function redirect($url, $args = []) {
        header('Location: '. $this->req->url($url, $args));
    }


    /**
     * @param $name
     * @param string $value
     * @return mixed
     */
    final public function session($name, $value = '') {
        return $this->req->session($name, $value);
    }
}